package gui;

import ge.escola.Laboratorio;
import ge.util.Validadores;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class CadastraLaboratorioPanel extends javax.swing.JPanel {

	private String nomeDoLaboratorio;
	private int numeroDeComputadores;
	private int capacidadeDeAlunos;
	private String horarioDeFuncionamentoInicial;
	private String horarioDeFuncionamentoFinal;
	private boolean flag = true;
	Color verde = new Color(0, 150, 0);

	private JLabel textoSuperior;
	private JTextField horaFuncInitB;
	private JLabel jLabel1;
	private JLabel doisPontos;
	private JTextField capAluLabTxt;
	private JTextField numCompLabTxt;
	private JLabel caixaMaior;
	private JLabel validaHoraFinit;
	private JLabel validaHoraInit;
	private JLabel validaCapAluno;
	private JLabel validaNumComp;
	private JTextField horaFuncFinalB;
	private JTextField horaFuncFinalA;
	private JLabel jLabel2;
	private JLabel validaNomeLab;
	private JButton buttonCadastrar;
	private JButton buttonLimpar;
	private JButton buttonCancelar;
	private JTextField horaFuncInitA;
	private JTextField nomeLabTxt;
	private JLabel horaFuncLab;
	private JLabel capAlunosLab;
	private JLabel numCompLab;
	private JLabel nomeLab;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
	}

	public CadastraLaboratorioPanel() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(800, 600));
			this.setSize(800, 600);
			this.setBackground(new java.awt.Color(209, 209, 255));
			this.setLayout(null);
			{
				textoSuperior = new JLabel();
				this.add(textoSuperior);
				textoSuperior
						.setText("                      Cadastro de Laboratorios");
				textoSuperior.setFont(new java.awt.Font("Arial", 0, 36));
				textoSuperior.setForeground(new java.awt.Color(0, 102, 102));
				textoSuperior.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				textoSuperior.setBounds(12, 39, 776, 74);
			}
			{
				nomeLab = new JLabel();
				this.add(nomeLab);
				nomeLab.setText("Nome do Laboratorio");
				nomeLab.setBounds(170, 235, 175, 21);
				nomeLab.setFont(new java.awt.Font("Arial", 0, 18));
				nomeLab.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				numCompLab = new JLabel();
				this.add(numCompLab);
				numCompLab.setText("Numero de computadores do laboratorio");
				numCompLab.setBounds(18, 277, 327, 21);
				numCompLab.setFont(new java.awt.Font("Arial", 0, 18));
				numCompLab.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				capAlunosLab = new JLabel();
				this.add(capAlunosLab);
				capAlunosLab.setText("Capacidade de aluno no laboratorio");
				capAlunosLab.setBounds(53, 320, 292, 21);
				capAlunosLab.setFont(new java.awt.Font("Arial", 0, 18));
				capAlunosLab.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				horaFuncLab = new JLabel();
				this.add(horaFuncLab);
				horaFuncLab.setText("Horario de funcionamento incial");
				horaFuncLab.setBounds(79, 362, 266, 21);
				horaFuncLab.setFont(new java.awt.Font("Arial", 0, 18));
				horaFuncLab.setForeground(new java.awt.Color(0, 102, 102));
			}
			{
				nomeLabTxt = new JTextField();
				this.add(nomeLabTxt);
				nomeLabTxt.setBounds(368, 236, 279, 23);
				nomeLabTxt.setFont(new java.awt.Font("Arial", 0, 14));
				nomeLabTxt.setForeground(new java.awt.Color(192, 192, 192));
				nomeLabTxt.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						nomeLabTxtKeyReleased(evt);
					}
				});
			}
			{
				numCompLabTxt = new JTextField();
				this.add(numCompLabTxt);
				numCompLabTxt.setBounds(368, 278, 123, 23);
				numCompLabTxt.setFont(new java.awt.Font("Arial", 0, 14));
				numCompLabTxt.setForeground(new java.awt.Color(192, 192, 192));
				numCompLabTxt.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						numCompLabTxtKeyReleased(evt);
					}
				});
			}
			{
				capAluLabTxt = new JTextField();
				this.add(capAluLabTxt);
				capAluLabTxt.setBounds(368, 321, 123, 23);
				capAluLabTxt.setFont(new java.awt.Font("Arial", 0, 14));
				capAluLabTxt.setForeground(new java.awt.Color(192, 192, 192));
				capAluLabTxt.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						capAluTxtKeyReleased(evt);
					}
				});
			}
			{
				jLabel1 = new JLabel();
				this.add(jLabel1);
				jLabel1.setText("Horario de funcionamento final");
				jLabel1.setFont(new java.awt.Font("Arial", 0, 18));
				jLabel1.setForeground(new java.awt.Color(0, 102, 102));
				jLabel1.setBounds(96, 407, 249, 21);
			}
			{
				horaFuncInitA = new JTextField();
				this.add(horaFuncInitA);
				horaFuncInitA.setBounds(368, 363, 39, 23);
				horaFuncInitA.setFont(new java.awt.Font("Arial", 0, 12));
				horaFuncInitA.setForeground(new java.awt.Color(192, 192, 192));
				horaFuncInitA.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						horaFuncInitKeyReleased(evt);
					}
				});
				horaFuncInitA.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						if (!horaFuncInitA.isEditable()) {
							horaFuncInitA.setText("");
							horaFuncInitA.setEditable(true);
						}
					}
				});

			}
			{
				doisPontos = new JLabel();
				this.add(doisPontos);
				doisPontos.setText(":");
				doisPontos.setBounds(413, 365, 10, 16);
				doisPontos.setFont(new java.awt.Font("Arial", 0, 18));
			}
			{
				horaFuncInitB = new JTextField();
				this.add(horaFuncInitB);
				horaFuncInitB.setBounds(425, 363, 39, 23);
				horaFuncInitB.setFont(new java.awt.Font("Arial", 0, 12));
				horaFuncInitB.setForeground(new java.awt.Color(192, 192, 192));
				horaFuncInitB.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						horaFuncInitKeyReleased(evt);
					}
				});
				horaFuncInitB.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						if (!horaFuncInitB.isEditable()) {
							horaFuncInitB.setText("");
							horaFuncInitB.setEditable(true);
						}
					}
				});
			}
			{
				horaFuncFinalA = new JTextField();
				this.add(horaFuncFinalA);
				horaFuncFinalA.setBounds(368, 408, 39, 23);
				horaFuncFinalA.setFont(new java.awt.Font("Arial", 0, 14));
				horaFuncFinalA.setForeground(new java.awt.Color(192, 192, 192));
				horaFuncFinalA.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						horaFuncFinalKeyReleased(evt);
					}
				});
				horaFuncFinalA.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						if (!horaFuncFinalA.isEditable()) {
							horaFuncFinalA.setText("");
							horaFuncFinalA.setEditable(true);
						}
					}
				});
			}
			{
				jLabel2 = new JLabel();
				this.add(jLabel2);
				jLabel2.setText(":");
				jLabel2.setFont(new java.awt.Font("Arial", 0, 18));
				jLabel2.setBounds(413, 403, 12, 29);
			}
			{
				horaFuncFinalB = new JTextField();
				this.add(horaFuncFinalB);
				horaFuncFinalB.setBounds(425, 407, 39, 23);
				horaFuncFinalB.setFont(new java.awt.Font("Arial", 0, 14));
				horaFuncFinalB.setForeground(new java.awt.Color(192, 192, 192));
				horaFuncFinalB.addKeyListener(new KeyAdapter() {
					public void keyReleased(KeyEvent evt) {
						horaFuncFinalKeyReleased(evt);
					}
				});
				horaFuncFinalB.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						if (!horaFuncFinalB.isEditable()) {
							horaFuncFinalB.setText("");
							horaFuncFinalB.setEditable(true);
						}
					}
				});
			}
			{
				buttonCancelar = new JButton();
				this.add(buttonCancelar);
				buttonCancelar.setText("CANCELAR");
				buttonCancelar.setBackground(new java.awt.Color(255, 255, 255));
				buttonCancelar.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				buttonCancelar.setBounds(673, 544, 115, 24);
				buttonCancelar.setFont(new java.awt.Font("Arial", 0, 10));
				buttonCancelar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonCancelarMouseClicked(evt);
					}
				});
			}

			{
				buttonLimpar = new JButton();
				this.add(buttonLimpar);
				buttonLimpar.setText("LIMPAR");
				buttonLimpar.setBackground(new java.awt.Color(255, 255, 255));
				buttonLimpar.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				buttonLimpar.setBounds(536, 544, 115, 24);
				buttonLimpar.setFont(new java.awt.Font("Arial", 0, 10));
				buttonLimpar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						buttonLimparMouseClicked(evt);
					}
				});
			}
			{
				buttonCadastrar = new JButton();
				this.add(buttonCadastrar);
				buttonCadastrar.setText("CADASTRAR");
				buttonCadastrar
						.setBackground(new java.awt.Color(255, 255, 255));
				buttonCadastrar.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
				buttonCadastrar.setBounds(399, 544, 115, 24);
				buttonCadastrar.setFont(new java.awt.Font("Arial", 0, 10));
				buttonCadastrar.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {

						try {
							// cria o laboratorio
							Laboratorio lab = new Laboratorio(
									nomeDoLaboratorio, numeroDeComputadores,
									capacidadeDeAlunos,
									horarioDeFuncionamentoInicial,
									horarioDeFuncionamentoFinal);
							// adiciona no array
							GerenciadorDeJanelas.todosLabs.add(lab);
							// transforma em xml
							String xmlLab = GerenciadorDeJanelas.ARQUIVADOR
									.toXML(GerenciadorDeJanelas.todosLabs);
							// grava o arrayXml no arquivo
							GerenciadorDeJanelas.gravar(
									GerenciadorDeJanelas.arquivoDeLaboratorios,
									xmlLab);
							// limpa os campos
							buttonCancelarMouseClicked(evt);
						} catch (Exception e) {
							JOptionPane.showMessageDialog(null,
									"Laboratorio nao serah cadastrado !",
									"Dados invalidos",
									JOptionPane.ERROR_MESSAGE);
						}
					}
				});
			}
			{
				validaNomeLab = new JLabel();
				this.add(validaNomeLab);
				validaNomeLab.setBounds(683, 236, 94, 23);
			}
			{
				validaNumComp = new JLabel();
				this.add(validaNumComp);
				validaNumComp.setText("");
				validaNumComp.setForeground(Color.white);
				validaNumComp.setBounds(521, 278, 94, 23);
			}
			{
				validaCapAluno = new JLabel();
				this.add(validaCapAluno);
				validaCapAluno.setText("");
				validaCapAluno.setForeground(Color.white);
				validaCapAluno.setBounds(521, 321, 94, 23);
			}
			{
				validaHoraInit = new JLabel();
				this.add(validaHoraInit);
				validaHoraInit.setText("");
				validaHoraInit.setForeground(Color.white);
				validaHoraInit.setBounds(521, 363, 94, 23);
			}
			{
				validaHoraFinit = new JLabel();
				this.add(validaHoraFinit);
				validaHoraFinit.setText("");
				validaHoraFinit.setForeground(Color.white);
				validaHoraFinit.setBounds(521, 407, 94, 23);
			}
			{
				caixaMaior = new JLabel();
				this.add(caixaMaior);
				caixaMaior.setBounds(12, 145, 776, 387);
				caixaMaior.setBorder(BorderFactory
						.createEtchedBorder(BevelBorder.LOWERED));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void nomeLabTxtKeyReleased(KeyEvent evt) {
		nomeDoLaboratorio = nomeLabTxt.getText();
		if (nomeDoLaboratorio.replace(" ", "").equals("")
				|| nomeDoLaboratorio == null) {
			validaNomeLab.setText("");
			validaNomeLab.setForeground(Color.white);
			flag = false;
		} else {
			validaNomeLab.setText("OK");
			validaNomeLab.setForeground(verde);
			flag = true;
		}
	}

	private void numCompLabTxtKeyReleased(KeyEvent evt) {
		String numComp = numCompLabTxt.getText();
		Pattern padrao = Pattern.compile("[0-9]*");
		Matcher meta = padrao.matcher(numComp);

		if (numComp.equals("") || numComp == null) {
			validaNumComp.setText("");
			validaNumComp.setForeground(Color.white);
			flag = false;

		} else if (!meta.matches()) {
			validaNumComp.setText("INVALIDO");
			validaNumComp.setForeground(Color.red);
			flag = false;
		} else if (Integer.parseInt(numComp) < 0) {
			validaNumComp.setText("INVALIDO");
			validaNumComp.setForeground(Color.red);
			flag = false;
		} else {
			validaNumComp.setText("OK");
			validaNumComp.setForeground(verde);
			numeroDeComputadores = Integer.parseInt(numComp);
			flag = true;
		}
	}

	private void capAluTxtKeyReleased(KeyEvent evt) {
		String capacidadeDeAlunos = capAluLabTxt.getText();
		Pattern padrao = Pattern.compile("[0-9]*");
		Matcher meta = padrao.matcher(capacidadeDeAlunos);

		if (capacidadeDeAlunos.equals("") || capacidadeDeAlunos == null) {
			validaCapAluno.setText("");
			validaCapAluno.setForeground(Color.white);
			flag = false;

		} else if (!meta.matches()) {
			validaCapAluno.setText("INVALIDO");
			validaCapAluno.setForeground(Color.red);
			flag = false;
		} else if (Integer.parseInt(capacidadeDeAlunos) < 0) {
			validaCapAluno.setText("INVALIDO");
			validaCapAluno.setForeground(Color.red);
			flag = false;
		} else {
			validaCapAluno.setText("OK");
			validaCapAluno.setForeground(verde);
			this.capacidadeDeAlunos = Integer.parseInt(capacidadeDeAlunos);
			flag = true;
		}
	}

	private void horaFuncInitKeyReleased(KeyEvent evt) {
		if (horaFuncInitA.getText().length() == 2) {
			horaFuncInitA.setEditable(false);
		}
		if (horaFuncInitB.getText().length() == 2) {
			horaFuncInitB.setEditable(false);
		}
		String duracao = horaFuncInitA.getText() + ":"
				+ horaFuncInitB.getText();
		if (!Validadores.validaHora(duracao)) {
			validaHoraInit.setText("Invalido");
			validaHoraInit.setForeground(Color.red);
			flag = false;
		} else {
			validaHoraInit.setForeground(verde);
			validaHoraInit.setText("Ok");
			horarioDeFuncionamentoInicial = duracao;
			flag = true;
		}
	}

	private void horaFuncFinalKeyReleased(KeyEvent evt) {
		if (horaFuncFinalA.getText().length() == 2) {
			horaFuncFinalA.setEditable(false);
		}
		if (horaFuncFinalB.getText().length() == 2) {
			horaFuncFinalB.setEditable(false);
		}
		String duracao = horaFuncFinalA.getText() + ":"
				+ horaFuncFinalB.getText();
		if (!Validadores.validaHora(duracao)) {
			validaHoraFinit.setText("Invalido");
			validaHoraFinit.setForeground(Color.red);
			flag = false;
		} else {
			validaHoraFinit.setForeground(verde);
			validaHoraFinit.setText("Ok");
			horarioDeFuncionamentoFinal = duracao;
			flag = true;
		}

	}

	private void limpar() {
		for (Component component : this.getComponents()) {
			if (component instanceof JTextField) {
				JTextField caixa = (JTextField) component;
				caixa.setText("");
				caixa.setEditable(true);
			}
		}
		validaCapAluno.setText("");
		validaHoraFinit.setText("");
		validaHoraInit.setText("");
		validaNomeLab.setText("");
		validaNomeLab.setText("");
		validaNumComp.setText("");
	}

	private void buttonLimparMouseClicked(java.awt.event.MouseEvent evt) {
		limpar();
	}

	private void buttonCancelarMouseClicked(java.awt.event.MouseEvent evt) {
		limpar();
		GerenciadorDeJanelas.window
				.setContentPane(GerenciadorDeJanelas.primeiroMenu);
		GerenciadorDeJanelas.window.setVisible(true);
	}

}
